package cn.edu.xjtu.android.emoji_warfare_android.engine;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import cn.edu.xjtu.android.emoji_warfare_android.engine.entity.Entity;
import cn.edu.xjtu.android.emoji_warfare_android.engine.shape.Circle;
import cn.edu.xjtu.android.emoji_warfare_android.engine.shape.Rectangle;
import cn.edu.xjtu.android.emoji_warfare_android.engine.shape.Shape;

/**
 * ScreenBoundary<br>
 * 屏幕边界
 * 
 * @author bill
 *
 */
public class ScreenBoundary extends Entity {
    /**
     * 宽度
     */
    private int width;
    /**
     * 高度
     */
    private int height;

    /**
     * 背景图片
     */
    private Bitmap background;

    /**
     * 构建屏幕边界
     * 
     * @param width
     *            宽度
     * @param height
     *            高度
     */
    public ScreenBoundary(int width, int height) {
        super(0, 0, new Rectangle(0, 0, width, height));
        this.width = width;
        this.height = height;
    }

    /**
     * 返回值表示是否在范围内，并确定碰撞的边缘<br>
     * 返回值是二进制格式，多个二进制位可以表示复合状态
     * 
     * @param obj 目标实体
     * @return 0表示在范围内，0001表示接触左边界，0010表示接触上边界，0100表示接触右边界，1000表示接触下边界
     */
    public int range(Entity obj) {
        float bx = basePoint.x;
        float by = basePoint.y;
        Shape s = obj.outline;
        float x = obj.basePoint.x;
        float y = obj.basePoint.y;
        int ans;
        if (s instanceof Rectangle) {
            Rectangle r = (Rectangle) s;
            ans = 0;
            if (y + r.y2 > by + height) {
                ans += 8;
            }
            if (x + r.x2 > bx + width) {
                ans += 4;
            }
            if (y + r.y1 < by) {
                ans += 2;
            }
            if (x + r.x1 < bx) {
                ans += 1;
            }
            return ans;
        } else if (s instanceof Circle) {
            float r = ((Circle) s).radius;
            ans = 0;
            if (y > by + height - r) {
                ans += 8;
            }
            if (x > bx + width - r) {
                ans += 4;
            }
            if (y < by + r) {
                ans += 2;
            }
            if (x < bx + r) {
                ans += 1;
            }
            return ans;
        }
        throw new IllegalArgumentException(
                String.format("Can't check is in the boundary for shape %s", s.getClass().getName()));
    }

    /**
     * 屏幕范围的宽度
     * 
     * @return 宽度
     */
    public int getWidth() {
        return width;
    }

    /**
     * 屏幕范围的高度
     * 
     * @return 高度
     */
    public int getHeight() {
        return height;
    }

    /**
     * 绘制背景图片
     */
    @Override
    public boolean onDraw(Canvas canvas) {
        if (background != null) {
            canvas.drawBitmap(background, 0, 0, bitmapPaint);
        }
        return false;
    }

    /**
     * 设置背景
     * 
     * @param background
     *            背景图片
     */
    public void setBackground(Bitmap background) {
        this.background = background;
    }

    /**
     * bitmap背景
     */
    private final static Paint bitmapPaint;
    static {
        bitmapPaint = new Paint();
        bitmapPaint.setAntiAlias(true);
        bitmapPaint.setFilterBitmap(true);
        bitmapPaint.setDither(true);
    }
}
